package com.smallkingson.ylib.core.utils;

import com.smallkingson.ylib.core.Yr;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;


public class YMath {

	public static void main(String[] args) {
		// System.out.println(randomInt(50, 70));
		System.out.println(randomFromInt(2, 5, 6, 8, 9));
	}

	public static String randomPhoneNum() {
		String s = "0123456789";
		String phoneNum = "";
		for (int i = 0; i < 8; i++) {
			phoneNum += s.charAt(YMath.randomInt(0, 10));
		}
		phoneNum = YMath.randomFromT("159", "158", "189", "135", "147")
				+ phoneNum;
		return phoneNum;
	}

	/**
	 * 给我几个参数，我就随机给你返回这些参数中的一个
	 * 
	 * @param nums
	 * @return
	 */
	public static int randomFromInt(int... nums) {
		if (nums.length == 0) {
			try {
				throw new Exception("没有参数啊");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return nums[randomInt(0, nums.length)];
	}

	/**
	 * 给我几个参数，我就随机给你返回这些参数中的一个
	 * 
	 * @param contents
	 * @return
	 */
	public static <T> T randomFromT(T... contents) {
		if (contents.length == 0) {
			try {
				throw new Exception("没有参数啊");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return contents[randomInt(0, contents.length)];
	}

	public static <T> T randomFromList(List<T> list) {

		return list.get(YMath.randomInt(0, list.size()));
	}

	public static <T> T randomFromArray(T[] contents) {
		if (contents.length == 0) {
			try {
				throw new Exception("没有参数啊");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return contents[randomInt(0, contents.length)];
	}

	/**
	 * 随机返回从start到end的整数 注意，不包含end这个数
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static int randomInt(int start, int end) {
		if (start > end) {
			try {
				throw new Exception("怎么起始数比结果数还大啊");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		int i;
		for (;;) {
			i = (int) (Math.random() * end);
			if (i >= start) {
				return i;
			}
		}
	}
	public static byte[] intToByteArray(int a) {
		return new byte[] {
				(byte) ((a >> 24) & 0xFF),
				(byte) ((a >> 16) & 0xFF),
				(byte) ((a >> 8) & 0xFF),
				(byte) (a & 0xFF)
		};
	}
	public static int byteArrayToInt(byte[] b) {
		return   b[3] & 0xFF |
				(b[2] & 0xFF) << 8 |
				(b[1] & 0xFF) << 16 |
				(b[0] & 0xFF) << 24;
	}
	public static short byteArrayToShort(byte[] b) {
		return (short) (b[1] & 0xFF |
            (b[0] & 0xFF) << 8);
	}
	public static String randomId() {
		long l = System.currentTimeMillis() << 4;
		return l + YMath.randomInt(0, 2 << 4) + "";
	}
	/**
	 * 计算百分比
	 * 
	 * @param currLen
	 * @param maxLen
	 * @return
	 */
	public static int percent(int currLen, int maxLen) {
		double d1 = currLen;
		double d2 = maxLen;
		return (int) ((d1 / d2) * 100);
	}

	/**
	 * 实现不重复的random
	 * 
	 * @author Administrator
	 * 
	 * @param <T>
	 */
	public static class NotRepeatArrayRandomer<T> {

		T[] objs;

		ArrayList<T> objects;

		public NotRepeatArrayRandomer(T[] objs) {

			this.objs = objs;

			objects = (ArrayList<T>) Arrays.asList(objs);

		}

		public NotRepeatArrayRandomer(List<T> objs) {

			objects = new ArrayList<T>();

			objects.addAll(objs);
		}

		public T random() {

			int index = YMath.randomInt(0, objects.size());

			T s = objects.get(index);

			objects.remove(index);

			return s;
		}
	}

	/**
	 * 把秒数转成当前时间
	 */
	public static Time formatSecond(int second) {

		Time time = new Time();
		int hour = 0, minute = 0;
		if (second >= 60) {
			time.minute = second / 60;
			time.second = second % 60;
			if (time.minute > 60) {
				time.hour = time.minute / 60;
				time.minute = time.minute % 60;

				if (time.hour > 24) {
					time.day = time.hour / 24;
					time.hour = time.hour % 60;
				}
			}
		}
		return time;
	}

	public static class Time {

		public int day;
		public int hour;
		public int minute;
		public int second;
	}
	
	/**
	 * 求有几种排列   比如  1 2 3  有 123 132 213 231 312 321 
	 * @param list
	 * @return
	 */
	/**
	 * 求有几种排列   比如  1 2 3  有 123 132 213 231 312 321 
	 * @param list
	 * @return
	 */
	public static <T> List<List<T>> sequentialCombination(List<T> list) {
		List<List<T>> returns = new ArrayList<List<T>>();

		print(list, 0, new ArrayList<T>(), returns);
		return returns;
	}

	public static <T> void print(List<T> input, int layer, List<T> result,
			List<List<T>> lists) {

		if (input.isEmpty()) { // 后面没排序的元素集合为空，输出一次result集合
			
			System.out.println(result);
			
			ArrayList<T> r=new ArrayList<T>(result);
			lists.add(r);
			System.out.println(lists);
			return;
		}

		int flag;
		for (int i = 0; i < input.size(); i++) {

			T node = input.get(i);

			flag = 0; // 同一位置，重复的数字只调用一次
			for (int n = 0; n < i; n++) {
				if (node == input.get(n)) {
					flag = 1;
					break;
				}
			}

			if (flag == 0) {
				if(result.size()>layer){
					result.remove(layer);
				}
				result.add(layer, node);
				List<T> newRefer = new LinkedList<T>();
				for (int j = 0; j < input.size(); j++) {
					newRefer.add(input.get(j));
				}
				newRefer.remove(i); // 深拷贝一个新的input集合，把正在处理的元素，从当前的input集合中拿掉
				print(newRefer, layer + 1, result, lists);
			}
		}
	}

}
